/**
 * 时间格式化功能函数
 * @param _time 时间戳
 * @param _format 格式化字符格式
 * @param _sem 是否语义化时间格式
 */
template.helper('formatTime', function(_time, _format, _sem){
    var _date,  _returnStr = '';
    _format = 'string' === typeof _format ? _format : 'MM/DD YY';
    _sem = 'boolean' === typeof _sem ? _sem : true;
    try{
        if('function' === typeof $.formatDate){
            _returnStr = $.formatDate(parseInt(_time), _sem, {
                _format : _format,
                _limit : $.FORMAT_FINAL.WEEK
            });
        }else{
            _date = new Date(parseInt(_time));
            _returnStr = _date.getFullYear() + '/' + (_date.getMonth() + 1) + '/' + _date.getDate();
        }
    }catch (e){
        _returnStr = '';
    }
    return _returnStr;
});
//时间格式化
template.helper('dataFormat', function(date,fmt){
    date = new Date(date);
    var o = {
        "M+" : date.getMonth()+1,                 //月份
        "d+" : date.getDate(),                    //日
        "h+" : date.getHours(),                   //小时
        "m+" : date.getMinutes(),                 //分
        "s+" : date.getSeconds(),                 //秒
        "q+" : Math.floor((date.getMonth()+3)/3), //季度
        "S"  : date.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
});
//性别格式化
template.helper('sexFormat', function(value){
    if(value == 2 || value == '2'){
        return "女";
    }else if(value == 1 || value == '1'){
        return "男";
    }else{
        return "未知";
    }
});
//列表页直播状态
template.helper("liveStatus", function (startTime,endTime,source_url) {
    if(!startTime || startTime==null ||startTime=='' || startTime <= 0){
        return '';
    }
    if(!endTime || endTime==null ||endTime=='' || endTime <= 0){
        return '';
    }
    var st = new Date(parseInt(startTime));
    var et = new Date(parseInt(endTime));
    var now = new Date();
    if(now.getTime() < st.getTime()){//未开始
        return '<div class="wjzBox_div_p2 un_living">未开始</div>';
    }else if(now.getTime() > st.getTime() && now.getTime() < et.getTime()){
        return '<div class="wjzBox_div_p2 living">直播中</div>';
    }else {
        if(source_url== null || source_url == '' || source_url == undefined){
            return '<div class="wjzBox_div_p2 un_living">已结束</div>';
        }else{
            return '<div class="wjzBox_div_p2 living">可回看</div>';
        }

    }
});
//详情页直播状态
template.helper("detailLiveStatus", function (startTime,endTime,source_url) {
    if(!startTime || startTime==null ||startTime=='' || startTime <= 0){
        return '';
    }
    if(!endTime || endTime==null ||endTime=='' || endTime <= 0){
        return '';
    }
    var st = new Date(parseInt(startTime));
    var et = new Date(parseInt(endTime));
    var now = new Date();
    if(now.getTime() < st.getTime()){//未开始
        return '<span class="status gray">未开始</span>';
    }else if(now.getTime() > st.getTime() && now.getTime() < et.getTime()){
        return '<span class="status red">直播中</span>';
    }else {
        if(source_url== null || source_url == '' || source_url == undefined){
            return '<span class="status gray">已结束</span>';
        }else{
            return '<span class="status red">可回看</span>';
        }

    }
});
//列表页直播状态
template.helper("isLive", function (startTime,endTime,source_url) {
    if(!startTime || startTime==null ||startTime=='' || startTime <= 0){
        return '';
    }
    if(!endTime || endTime==null ||endTime=='' || endTime <= 0){
        return '';
    }
    var st = new Date(parseInt(startTime));
    var et = new Date(parseInt(endTime));
    var now = new Date();
    if(now.getTime() < st.getTime()){//未开始
        return false;
    }else if(now.getTime() > st.getTime() && now.getTime() < et.getTime()){
        return true;
    }else {
        if(source_url== null || source_url == '' || source_url == undefined){
            return false;
        }else{
            return true;
        }

    }
});

//时间比较
template.helper("timeComp", function (startTime, endTime) {
    if(!startTime || startTime==null ||startTime=='' || startTime <= 0){
        return '';
    }
    if(!endTime || endTime==null ||endTime=='' || endTime <= 0){
        endTime = new Date().getTime();
    }
    var st = new Date(parseInt(startTime));
    var et = new Date(parseInt(endTime));
    if(st.getTime() < et.getTime()){
        return -1;
    }else if(st.getTime() === et.getTime()){
        return 0;
    }else{
        return 1;
    }
});


//金额处理
template.helper("dealAmount", function (amount) {
    if (typeof amount !== "number" || isNaN(amount)) {
        return null;
    }
    return (amount / 100).toFixed(2);
});
//json转换
template.helper("parseJson", function (jsonStr) {
    return JSON.parse(jsonStr);
});
//订单状态
template.helper("orderStatus", function (status) {
    if(status == 1){
        return '<div class="payBtn unpay">去支付</div>';
    }else if(status == 2){
        return '<div class="payBtn">已付款</div>';
    }else {
        return '<div class="payBtn">已取消</div>';
    }
});
//订单状态
template.helper("orderStatusText", function (status) {
    if(status == 1){
        return '未支付';
    }else if(status == 2){
        return '已支付';
    }else {
        return '已取消';
    }
});
function urlParam( url , name , value ) {
    var reg = new RegExp("(\\\?|&)" + name + "=([^&]+)(&|$)", "i");
    var m = url.match(reg);
    if (typeof value != ' undefined') { //赋值
        if (m) {
            return ( url.replace(reg, function ($0, $1, $2) {
                return ($0.replace($2, value));
            }));
        } else {
            if (url.indexOf('?') == -1) {
                return (url + '?' + name + '=' + value);
            } else {
                return (url + '&' + name + '=' + value);
            }
        }
    } else { //取值
        if (m) {
            return m[2];
        } else {
            return '';
        }
    }
}
//处理视频url宽高
template.helper("handleVideoUrl", function (url) {
    var protocolStr = document.location.protocol;
    if(protocolStr == "https:") {
        url = url.replace("http://","https://");
    }
    if(url != null && url != '' && url != undefined){
        url = urlParam(url,"width",document.body.clientWidth);
        url = urlParam(url,"height",document.body.clientWidth*9/16);
    }
    return url;
});

template.helper('formatDouble', function(value, num){
    try {
        if (typeof value !== Number) {
            value = Number(value)
        }
        return value.toFixed(num);
    }catch (e){
        console.log(e)
    }
});

template.helper('parseFor', function(value){
    var numArr = [];
    for(var i = 0; i < value; i++){
        numArr[i] = {id: i + 1};
    }
    return numArr;
});

/**
 * 奖项级别
 */
template.helper('prizeType', function(_value){
    return prizeType(_value);
});

function prizeType(_value) {
    var _returnStr = '';
    switch (parseInt(_value)){
        case 1 : _returnStr = "一等奖"; break;
        case 2 : _returnStr = "二等奖"; break;
        case 3 : _returnStr = "三等奖"; break;
        case 4 : _returnStr = "四等奖"; break;
        case 5 : _returnStr = "五等奖"; break;
        case 6 : _returnStr = "六等奖"; break;
        case 99 : _returnStr = "未中奖"; break;
    }
    return _returnStr;
}